<?php 
if (!defined('BASEPATH')){
	exit('No direct script access allowed');
}

/**
 *  ListcustomersDebts_model Model
 *
 */

class ListcustomersDebts_model extends MY_Model{

	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_contacts';
	}
        
	public function init_data($right){
		return array(
				array(
						'name' => 'name',
						'label' => $this->lang->line('name'),
						'width' => '10%',
						'sort'  => 'ASC',
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'customerID',
						'label' => $this->lang->line('customerID'),
						'width' => '20%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'address',
						'label' => $this->lang->line('address'),
						'width' => '20%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
				array(
						'name' 	=> 'phone',
						'label' => $this->lang->line('phone'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'type_customers',
						'label' => $this->lang->line('type_customers'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'surplus',
						'label' => $this->lang->line('surplus'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'username',
						'label' => $this->lang->line('updatedby'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'note',
						'label' => $this->lang->line('note'),
						'width' => '15%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				)
		);
	}
	
	/*
	 * datatables data
	* return json
	*/
	public function json_data($controller, $right){
            
                if($this->session->userdata('s_listcustomersDebts')){
                    $s_listcustomersDebts = json_decode($this->session->userdata('s_listcustomersDebts'));
                }
                
		$this->datatables
		->select('c.name,c.customerID,c.address,c.phone,c.lastupdated,c.active,c.surplus,t.name AS type,ag.pcc AS agent_code,prov.name AS province,usr.username AS username,usr.id AS user_id')
                ->from($this->table_name . ' AS c')
                ->join('book_type_customers AS t','t.id = c.type')
                ->join('book_agent_code AS ag','c.agent_code = ag.id')
                ->join('book_province AS prov','prov.id = c.province')
                ->join('users AS usr','usr.id = c.owner')
                ->where('c.active = 1')
                ->where('c.surplus != 0 AND c.surplus IS NOT NULL');
                
                if(!in_array($this->session->userdata['group_id'],array('1','79'))){
                    $this->datatables->where('c.share LIKE ','%'.$this->session->userdata['user_id'] .'%');
                }
                
                if($this->session->userdata['agent_code']){
                    $this->datatables->where('c.agent_code = '.$this->session->userdata['agent_code']);
                }
                
                if(isset($s_listcustomersDebts) && $s_listcustomersDebts->s_customerName != ''){
                    $this->datatables->where('c.name REGEXP ("'.$s_listcustomersDebts->s_customerName.'")');
                }
                
                if(isset($s_listcustomersDebts) && $s_listcustomersDebts->s_phone != ''){
                    $this->datatables->like('c.phone',$s_listcustomersDebts->s_phone,'both');
                }
                
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();
                
                $this->session->unset_userdata('s_listcustomersDebts');

		foreach($datatables['aaData'] as $item){
                        $noteSurplus       = $this->listcustomersDebts_model->getInfoTable(array('customerID'=>$item['customerID']),'book_payment_log');
                        
			$ouput['aaData'][] = array(
					$item['name'],
					$item['customerID'],
                            		$item['address'],
					implode("<br/>", json_decode($item['phone'])),
                                        $item['type'],
                                        $this->bookinglib->my_number_format($item['surplus'],2, ',', ','),
                                        $item['username'],
                                        ($noteSurplus)?$noteSurplus[0]->note:''
			);
                        unset($noteSurplus);
		}
	
		return json_encode($ouput);
	}
}